package cn.jn.lhm.netty.part04;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * @author lhm 2753914576@qq.com
 * @time 2023/7/12 - 22:39
 **/
@Slf4j
public class TestNettyFuture {
    @SuppressWarnings("all")
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group = new NioEventLoopGroup(2);

        EventLoop executor = group.next();


        Future<String> result = executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("返回结果....");
            return "aaa";
        });

        // 同步
        // log.info("获取结果:{}", result.get());

        // 异步
        result.addListener(future -> {
            log.info("获取结果:{}", future.getNow());
            group.shutdownGracefully();
        });
    }
}
